package com.example.work4

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.adapter.FriendListAdapter
import com.example.work4.http.FriendRetrofitImp
import com.example.work4.http.data.FriendList
import com.example.work4.po.ResultBean
import com.example.work4.view.StatusBar
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class FriendActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_friend)
        StatusBar.setStatusBarAsBlue(window)
        init()
    }

    var type = ""
    var friendRetro = FriendRetrofitImp()

    fun init(){
        type = intent.getStringExtra("type")!!
        val recyclerView = findViewById<RecyclerView>(R.id.messageRecyclerView);
        val linearLayoutManager = LinearLayoutManager(this);
        recyclerView!!.layoutManager = linearLayoutManager
        val adapter = FriendListAdapter(ArrayList<FriendList.FriendBaseData>())
        initAdapter(adapter,recyclerView)

    }

    private fun initAdapter(adapter: FriendListAdapter, recyclerView: RecyclerView) {
        friendRetro.listCall().enqueue(object : Callback<ResultBean<FriendList>> {
            override fun onResponse(call: Call<ResultBean<FriendList>>,
                                    response: Response<ResultBean<FriendList>>) {
                if(response.code()== 200){
                    val list: FriendList? = response.body()!!.data
                    val listName = findViewById<TextView>(R.id.list_name)
                    when(type){
                        "fan" ->{
                            listName.text = "粉丝列表"
                            adapter.data = list!!.fanList
                        }
                        "follow"->{
                            listName.text = "关注列表"
                            adapter.data = list!!.followList
                        }
                        "friend"->{
                            listName.text = "朋友列表"
                            adapter.data = list!!.friendList
                        }
                    }
                    recyclerView.adapter = adapter
                }
            }
            override fun onFailure(call: Call<ResultBean<FriendList>>, t: Throwable) {

            }
        })
    }


}